package com.ztz.musicappdemo.activity;

import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.ztz.musicappdemo.MyDbHelper;
import com.ztz.musicappdemo.R;
import com.ztz.musicappdemo.adapter.UserInfoAdapter;
import com.ztz.musicappdemo.bean.UserInfo;

import java.util.ArrayList;
import java.util.List;

public class UserInfoManageActivity extends BaseActivity {
    ListView lv_user_Info;
    UserInfoAdapter adapter;
    List<UserInfo> userInfoList=new ArrayList<>();
    MyDbHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info_manage);
        lv_user_Info=(ListView)findViewById(R.id.lv_userInfo);
        dbHelper=new MyDbHelper(this,"UserInfoStore.db",null,1);

        userInfoList.add(new UserInfo(1,"li","3333","13605286215","男","kit"));
        userInfoList.add(new UserInfo(2,"mike","3511","18252363214","女","漂亮"));
        adapter=new UserInfoAdapter(this,userInfoList);
        lv_user_Info.setAdapter(adapter);
        initReadUserInfo();
        lv_user_Info.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(final AdapterView<?> parent, View view, final int position, long id) {
                final UserInfo userInfo=userInfoList.get(position);
                AlertDialog.Builder builder=new AlertDialog.Builder(UserInfoManageActivity.this)
                        .setTitle("删除用户")
                        .setMessage("用户名"+userInfo.getUserName()+"\n"+"密码"+userInfo.getPassword()+"\n"+"电话号码"+userInfo.getPhone()
                        +"\n"+"性别"+userInfo.getSex())
                        .setCancelable(false)
                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                SQLiteDatabase db=dbHelper.getWritableDatabase();
                                db.delete("UserInfo",null,null);
                                userInfoList.remove(position);
                                adapter.notifyDataSetChanged();
                                Toast.makeText(UserInfoManageActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(UserInfoManageActivity.this, "你取消了操作", Toast.LENGTH_SHORT).show();
                            }
                        });
                builder.show();
            }
        });
    }

    private void initReadUserInfo() {
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        Cursor cursor= db.query("UserInfo",null,null,null,null,null,null);
        while (cursor.moveToNext()){
            int id=cursor.getInt(0);
            String userName=cursor.getString(1);
            String password=cursor.getString(2);
            String phone=cursor.getString(3);
            String sex=cursor.getString(4);
            String content=cursor.getString(5);

            userInfoList.add(new UserInfo(id,userName,password,phone,sex,content));
        }
       adapter.notifyDataSetChanged();
    }
}
